//
//  SoucangViewController.m
//  Ten
//
//  Created by anfuer on 2018/8/21.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "SoucangViewController.h"
#import "companyCell.h"
#import "HomeTbvCell.h"
#import "SPPageMenu.h"
#import "HomeTbvCell.h"
@interface SoucangViewController ()<SPPageMenuDelegate, UIScrollViewDelegate>
{
    int pag;
    NSMutableArray*mary1;
    NSMutableArray*mary2;
}
@property (weak, nonatomic) IBOutlet UIView *muneBack;
@property (weak, nonatomic) IBOutlet UIScrollView *scrview;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrW;
@property (weak, nonatomic) IBOutlet UITableView *tbv1;
@property (weak, nonatomic) IBOutlet UITableView *tbv2;
@property (weak, nonatomic) IBOutlet UIView *hidenView1;
@property (weak, nonatomic) IBOutlet UIView *hidenView2;
@property (nonatomic, weak) SPPageMenu *pageMenu;

@end

@implementation SoucangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _tbv1.separatorColor=[UIColor clearColor];
    _tbv2.separatorColor=[UIColor clearColor];
    _scrW.constant=DEVW*2;
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, DEVW, 40) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    // 传递数组，默认选中第2个
    [pageMenu setItems:@[@"职位",@"公司"] selectedItemIndex:0];
    pageMenu.permutationWay=SPPageMenuPermutationWayNotScrollEqualWidths;
    // 设置代理
    pageMenu.delegate = self;
    [_muneBack addSubview:pageMenu];
    _pageMenu = pageMenu;
    
    
    _tbv1.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pag=1;
        mary1=[NSMutableArray array];
        [self loadData];
    }];
    _tbv2.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pag=1;
        mary2=[NSMutableArray array];
        [self loadData];
    }];
    // Do any additional setup after loading the view.
}
-(void)loadData{
    [FuncPublic getData:self parameters:@{@"type":[NSString stringWithFormat:@"%ld",_pageMenu.selectedItemIndex+1]} RequestUrl:Xmycollects success:^(NSDictionary *acountInfo) {
    
        [_tbv1.mj_header endRefreshing];
        [_tbv2.mj_header endRefreshing];
        if (_pageMenu.selectedItemIndex==0) {
            mary1=[NSMutableArray arrayWithArray:acountInfo[@"data"]];
            [_tbv1 reloadData];
            _hidenView1.hidden=mary1.count>0?YES:NO;
        }else
        {
            mary2=[NSMutableArray arrayWithArray:acountInfo[@"data"]];
            [_tbv2 reloadData];
            _hidenView2.hidden=mary2.count>0?YES:NO;
        }
        
    }];
    
}
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (labs(toIndex - fromIndex) >= 2) {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:NO];
    } else {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:YES];
    }
    pag=1;
    mary1=[NSMutableArray array];
    mary2=[NSMutableArray array];
    [self loadData];
}

#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView==_scrview) {
        [self.pageMenu moveTrackerFollowScrollView:scrollView];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pageMenu.selectedItemIndex==0? mary1.count:mary2.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (_pageMenu.selectedItemIndex==0) {
        HomeTbvCell*cell=[HomeTbvCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell Cellinit:mary1[indexPath.row][@"job_detail"]];
        return cell;
    }else
    {
        companyCell*cell=[companyCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell Cellinit:mary2[indexPath.row][@"company_detail"]];
        return cell;
    }

    
}

- (IBAction)CleanBtna:(UIButton *)sender {
    
    
}

#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return _pageMenu.selectedItemIndex==0?[HomeTbvCell FSCellHeightForTableView:tableView indexPath:indexPath cacheKey:@"HomeTbvCell" cellContentViewWidth:0 bottomOffset:0]:70;
    
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (_pageMenu.selectedItemIndex==0) {
        [MyUtils collviewSelidex:@"职位详情" toVc:self andShopInfo:mary1[indexPath.section]];
    }else
    {
        [MyUtils collviewSelidex:@"公司详情" toVc:self andShopInfo:mary2[indexPath.section][@"company_detail"]];
    }
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
//进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    
        
        
    }
}
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


@end
